package com.mayy.spark.demo;

import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SparkSession;

/**
 * 配置远程调试
 * VM Options添加：
 * -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
 */
public class SparkRemoteDebug {
    public static void main(String[] args) {
        // 添加调试检查点
        System.out.println("Debug session started...");

        SparkSession spark = SparkSession.builder()
                .appName("Remote Debug")
                .master("local[*]")
                .config("spark.driver.extraJavaOptions",
                        "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005")
                .getOrCreate();

        // 你的业务代码
        debugYourSparkLogic(spark);

        spark.stop();
    }

    private static void debugYourSparkLogic(SparkSession spark) {
        // 在这里设置断点
        Dataset<Long> data = spark.range(10);

        // 逐步调试转换操作
        Dataset<Long> result = data.map((MapFunction<Long, Long>) x -> {
            // 调试lambda函数
            Long processed = x * 10 + 5;
            System.out.println("Debug lambda: " + x + " -> " + processed);
            return processed;
        }, Encoders.LONG());

        result.show();
    }
}
